/*
 * @# DemoController.java 2011-12-13 下午02:34:08
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.test;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 命名规则: 
 * 
 * (方法名) create, edit[save, update], remove, removeAll, show
 * 
 * Controller(类/路径名) : "/xx/*" => XxController
 * 
 * @author alvin hwang
 */
@Controller
@SessionAttributes("userId")
@RequestMapping("/demo/*") // 对整个类的路径定义(也可以分别定义在每个方法中)
public class DemoController {

	@RequestMapping("simple")
	public @ResponseBody String simple() {
		return "Hello world!";
	}
	
	@RequestMapping(value="ajax", method=RequestMethod.POST)
	public @ResponseBody String postAjax(JavaBean javaBean) {
		return "Bean: " + ToStringBuilder.reflectionToString(javaBean);
	}
	
	// failed
	@RequestMapping(value="json", method=RequestMethod.POST)
	public @ResponseBody String readJson(@RequestBody JavaBean bean) {
		return "Read from JSON: " + bean;
	}
	
	@RequestMapping(value="json", method=RequestMethod.GET)
	public @ResponseBody JavaBean writeJson() {
		JavaBean javaBean = new JavaBean();
		javaBean.setFoo("foo-1");
		javaBean.setTee("tee-1");
		return javaBean;
	}
	
	@RequestMapping(value="json2", method=RequestMethod.GET)
	public @ResponseBody JavaBean writeJson2() {
		JavaBean javaBean = new JavaBean();
		javaBean.setFoo("中文");
		javaBean.setTee("tee-1");
		return javaBean;
	}
	
	@RequestMapping(value="list", method=RequestMethod.GET)
	public @ResponseBody List<String> writeList() {
		List<String> list = new ArrayList<String>();
		list.add("中文");
		list.add("tee-1");
		return list;
	}
	
	@RequestMapping(value="view", method=RequestMethod.GET)
	public void view(Model model) {
		model.addAttribute("foo", "foo value");
		model.addAttribute("tee", "tee value");
	}
	
	@RequestMapping(value="view2", method=RequestMethod.GET)
	public String view2(Model model) {
		model.addAttribute("foo", "foo value");
		model.addAttribute("tee", "tee value");
		return "demo/view";
	}
	
	@RequestMapping(value="view3/{foo}/{tee}", method=RequestMethod.GET)
	public String view3(@PathVariable String foo, @PathVariable String tee) {
		// No need to add @PathVariables "foo" and "tee" to the model
		// They will be merged in the model before rendering
		// 后面的参数自动加入到Model中
		return "demo/view";
	}
	
	@RequestMapping(value="view11/{foo}/{tee}", method=RequestMethod.GET)
	public String view4(@PathVariable("foo") String foo, @PathVariable String tee, Model model) {
		model.addAttribute("foo", foo);
		model.addAttribute("tee", tee);
		return "demo/view";
	}
	
	@RequestMapping(value="view4/{foo}/{tee}", method=RequestMethod.GET)
	public String view4(@PathVariable("foo") String foo, @PathVariable String tee, @RequestParam("a") String a,
			Model model) {
		model.addAttribute("aa", a);
		return "demo/view";
	}
	
	@RequestMapping(value="view10/{foo}/{tee}", method=RequestMethod.GET)
	public String view4(@PathVariable("foo") String foo, @PathVariable String tee, @RequestParam("a") String a) {
		return "demo/view";
	}
	
	@RequestMapping(value="view5/{foo}/{tee}", method=RequestMethod.GET)
	public String view5(JavaBean javaBean) {
		// 后面的参数自动加入到javaBean中
		return "demo/view2";
	}
	
	@RequestMapping(value="view51", method=RequestMethod.GET)
	public String view51(JavaBean javaBean) {
		// 后面的参数自动加入到javaBean中
		return "demo/view2";
	}
	
	@RequestMapping(value="view6/{foo}/{tee}", method=RequestMethod.GET)
	public String view6(JavaBean javaBean, Model model) {
		// 自定义model里的key
		model.addAttribute("javaBean", javaBean);
		model.addAttribute("goo", "goooo");
		return "demo/view2";
	}
	
	@RequestMapping(value="view6/{foo}/{tee}/{goo}", method=RequestMethod.GET)
	public String view61(JavaBean javaBean, @PathVariable("goo") String foo, Model model) {
		return "demo/view2";
	}
	
	@RequestMapping(value="view7/{foo}/{tee}", method=RequestMethod.GET)
	public ModelAndView view7(@PathVariable String foo, @PathVariable String tee) {
		JavaBean javaBean = new JavaBean();
		javaBean.setFoo(foo);
		javaBean.setTee(tee);
		return new ModelAndView("demo/view2", "javaBean", javaBean);
	}
	
	@RequestMapping(value="create", method=RequestMethod.GET)
	public ModelAndView create() {
		JavaBean javaBean = new JavaBean();
		javaBean.setFoo("foo-1");
		javaBean.setTee("tee-1");
		ModelAndView modelAndView = new ModelAndView("demo/form", "javaBean", javaBean);
		modelAndView.addObject("goo", "adaaaa");
		return modelAndView;
	}
	
	@RequestMapping(value="form", method=RequestMethod.GET)
	public String form() {
		return "demo/form";
	}
	
	@RequestMapping(value="save", method=RequestMethod.POST)
	public String save(JavaBean javaBean, RedirectAttributes redirectAttrs) {
		System.out.println("==============foo : " + javaBean.getFoo());
		System.out.println("==============tee : " + javaBean.getTee());
		redirectAttrs.addFlashAttribute("javaBean", javaBean);
		return "redirect:/demo/form";
	}
	
	@RequestMapping(value="save2", method=RequestMethod.POST)
	public String save2(@ModelAttribute("javaBean") JavaBean javaBean) {
		System.out.println("==============foo : " + javaBean.getFoo());
		System.out.println("==============tee : " + javaBean.getTee());
		return "redirect:/demo/create";
	}
	
	@RequestMapping(value="session", method=RequestMethod.GET)
	public String session(HttpSession session) {
		session.setAttribute("userId", "user 1");
		return "demo/session";
	}
	
	@RequestMapping(value="session2", method=RequestMethod.GET)
	public String session2(@ModelAttribute("userId") String userId) {
		// 配合上面@SessionAttributes("userId")进行使用
		System.out.println("==========session userId : " + userId);
		return "demo/session";
	}
	
}
